package view{
	
	import data.Tower;
	
	import events.GameEvent;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	
	import manager.TdManage;
	
	import utils.Image;
	
	/**
	 * 显示控制 类
	 * @author 1
	 * 
	 */	
	public class MapView extends Sprite{
		/**
		 * 声明数据控制对象 
		 */		
		private var manage:TdManage;
		private var buildBar:BuildBar;
		
		private var box:Sprite = new Sprite();
		private var buildSelect:BuildSelect;
		
		/**
		 * 塔的升级菜单 
		 */		
		private var upgradeBar:UpgradeBar = UpgradeBar.getInstence();

		private var towerRange:Sprite;
		
		public function MapView(){
			if(stage) inits();
			else addEventListener(Event.ADDED_TO_STAGE,inits);
		}
		
		private function inits(event:Event=null):void{
			
			//添加背景图片
			var mapBig:Image = new Image();
			addChild(mapBig);
			mapBig.source = "assets/map1.png";
			
			mapBig.addEventListener(MouseEvent.CLICK,bgClickHandle);
			
			box = new Sprite();
			addChild(box);
			
			
			
			towerRange = new Sprite();
			box.addChild(towerRange);
//			box.setChildIndex(towerRange,box.numChildren-1);
			
			buildSelect = new BuildSelect();
			box.addChild(buildSelect);
			
			manage = TdManage.getInstance();
			box.addChild(manage);
			
			buildBar = BuildBar.getInstence();
			box.addChild(buildBar);
			buildBar.visible = false;
			
			manage.addEventListener(TimerEvent.TIMER, onTimer);
			
			//侦听建筑物选择器上的建筑物类型的点击事件,此事件触发说明有塔要被建立
			buildBar.addEventListener(GameEvent.CREAT_BUILD_VIEW,creatBuildViewHandle);
			
			buildBar.addEventListener(GameEvent.ATTACK_RANGE,attackRangeHandle);
			buildBar.addEventListener(GameEvent.CLEAR_TOWERRANGE,clearRangeHandle);
			
			buildSelect.addEventListener(GameEvent.BUILDBAR_VIEW,buildBarViewHandle);
			
			//建筑物被单击
			addEventListener(GameEvent.UPGRADEBAR_SHOW,upgradeBarHandle);
			
			
			
			
		}
		
		/**
		 * 清除塔的攻击范围显示 
		 * @param event
		 * 
		 */		
		private function clearRangeHandle(event:Event):void{
			if(towerRange.visible){
//				box.removeChild(towerRange);
				towerRange.visible = false;
			}
		}
		
		/**
		 * 显示塔的攻击范围 
		 * @param event
		 * 
		 */		
		private function attackRangeHandle(event:GameEvent):void{
			var obj:Object = event.obj;
			drawRange(obj);
			towerRange.visible = true;
		}
		
		private function drawRange(_obj:Object):void{
			var point:Point = _obj.p;
			var r:Number = _obj.r;
			towerRange.graphics.clear();
			towerRange.graphics.lineStyle(1,0x879F2F);
			towerRange.graphics.beginFill(0x879F2F,.2);
			towerRange.graphics.drawCircle(point.x,point.y,r);
			towerRange.graphics.endFill();
			
		}
		
		private function upgradeBarHandle(event:GameEvent):void{
			var mc:MovieClip = event.obj as MovieClip;
			trace(mc);
			upgradeBar.x = mc.x;
			upgradeBar.y = mc.y;
			box.addChild(upgradeBar);
			box.setChildIndex(upgradeBar,box.numChildren-1);
			if(!upgradeBar.visible){
				upgradeBar.orShow();
			}
		}
		
		/**
		 * 当地图图片被单击时 
		 * @param event
		 * 
		 */		
		private function bgClickHandle(event:MouseEvent):void{
			buildBar.setHid();
			if(upgradeBar.visible){
				upgradeBar.orShow();
			}
		}		
		
		
		protected function onTimer(event:Event):void{
			//dosomething;
		}
		
		/**
		 * 显示塔的建筑物选择器 
		 * @param event
		 * 
		 */		
		private function buildBarViewHandle(event:GameEvent):void{
			var img:Image = event.obj as Image;
			buildBar.x = img.x + 25;
			buildBar.y = img.y + 20;
			box.setChildIndex(buildBar,box.numChildren-1);
			buildBar.orShow();
			if(!buildBar.visible){
				buildBar.orShow();
			}
			if(upgradeBar.visible){
				upgradeBar.orShow();
			}
		}
		/**
		 * 创建所要显示的塔 
		 * @param event
		 * 
		 */		
		protected function creatBuildViewHandle(event:GameEvent):void{
			buildBar.orShow();
			var t:Tower = event.obj as Tower;
			var towerView:TowerView = new TowerView(t);
			box.addChild(towerView);
			
		}
		
		
	}
}